GBML manual / Rooms

Moving between rooms

A game is a set of rooms and only one is loaded at a time, so leaving a room means playing a transition, tearing the old room down, building the next, and choosing what, if anything, comes along.

One room at a time

A GBA game is not one big world in memory. It is a list of rooms, and exactly one is loaded at any moment. A room is one space with a size, some tiles, and the objects you placed in it. What a room is made of, and how you build one in the editor, is in Rooms and Rooms, tiles and tile animation. This page is about the other half: getting from one room to the next from your code.

In GBML a room is a value of the type room, and you name one by writing its name as a plain word: room_change(room_boss_arena). No quotes. The order of the rooms in your project matters for one function, room_next, described below.

The functions

FunctionWhat it does
room_change(r)Leaves the current room for room r.
room_change_ext(r, target, x, y, transition)The same, but also carries one instance along, places it at (x, y) (counted from the top-left corner of the room you are going to), and lets you pick the transition.
room_change_seamless(r, offset_x, offset_y, carry_within)Goes to room r with no fade and no transition. This instance and the camera come through moved by the offset, so the picture does not change. The fourth value is optional: every instance within that many pixels of this one comes through with it.
room_next()Goes to the next room in the project's order. After the last room it wraps back to the first.
room_restart()Reloads the current room from scratch, as if you had changed to it.
room_current()Which room is loaded right now.
room_width() / room_height()The current room's size in pixels.
room_left() / room_top() / room_right() / room_bottom()Where the current room's four edges are.
room_width_of(r) / room_height_of(r)The size of any room r, not only the current one.
room_set_shroud_darkness(percent)Changes how dark the current dark room is, while the game runs. The lights still cut through.

room_change used to be called room_goto. If you meet that name in an old project, it is the same thing under its new name.

A change is a short sequence, not an instant

Calling room_change does not swap the room on that line. It starts a transition, the effect that hides the swap: a fade, a wipe, a shrinking circle. A transition has three parts.

  1. Cover. The effect plays until the screen is fully hidden.
  2. Load. While nothing is visible, the old room is torn down and the new one is built.
  3. Reveal. The effect plays backward and the new room appears.

Ordinary gameplay is frozen for all of it. Step, Alarm, Button, Movement and Collision events do not run. Drawing, the camera and audio keep going.

The swap in step 2 happens in a fixed order:

  1. Every instance runs its Room End event.
  2. Every instance that is not persistent is destroyed. Persistent is explained below. (If the room being left is marked Persistent, they are set aside instead, and wait for the player: see Persistent rooms.)
  3. The new room is built. Each placed instance is made, one at a time: its object's own settings go on it, then its Create event runs, then any creation code you gave that copy. A door's own settings, the ones its dialog saved, land right after its Create.
  4. The room's own creation code runs, once every placed instance has been made.
  5. Every instance now in the room runs its Room Start event.

Coming back to a room the game kept is shorter: steps 3 and 4 do not happen, because nothing needs to be made. The instances set aside in step 2 are put back exactly as they were, and step 5 runs for them like for everyone else.

For the very first room of the game, Game Start also runs between steps 3 and 4. When each event runs has the full order, with a picture.

Two things can make room_change do nothing. It is ignored if a transition is already running, so calling it from a Button Pressed event cannot start two at once. It is also ignored if the room you named does not exist. Neither is an error.

Choosing the transition

There are fifteen transitions. Which one plays is decided by the room being left, not the one you are heading to. Each room has an on exit setting in the editor's Room Properties (Transitions and Values, the Transition list), and room_change and room_restart use it. A room that has never been set fades to black.

room_change_ext lets a single call override that. Its last argument is a transition written as a plain word:

KindTakes a direction?
instant_cut, fade_to_black, fade_to_white, white_flash, mosaic, circle_grow, circle_shrink, iris, diamond, tv_switch_offNo
venetian_blinds, comb, wavy_wipe, barn_doorsYes: .north, .east, .south, .west
diagonal_wipeYes: .northeast, .southeast, .southwest, .northwest

So wavy_wipe.north is a wavy wipe that sweeps north, and fade_to_black needs nothing more. A direction on a kind that takes none, such as fade_to_black.north, is a build error. Leaving the direction off, or writing .auto, picks the direction the player is traveling. In code, that is always south, so auto is never vague.

The circle and diamond shapes (Circle, Iris, Diamond, TV switch-off) close in on a spot, and the room's Focus setting picks it: the middle of the screen, or Player. In a built game the player is the instance your camera follows (the one you gave to camera_follow): the shape closes in on where that instance is on the screen when the change starts, and opens out from where it is in the new room. If the camera follows nothing, Player uses the middle of the screen, the same as Screen center.

A transition is not an ordinary value. You cannot store one in a variable or compare two of them. There is one exception: a door. Each door you place in the Room Editor has a transition of its own, chosen in that door's dialog, and the door's code reads it as self.send_transition. That is how two doors on the same wall can play different transitions from the same script.

Taking something along

Normally everything in the old room is destroyed on the way out. room_change_ext can rescue one instance, the target. The target comes through the swap and is placed at (x, y) in the new room before anything in it is created, so the new room's own code already finds it in place. Pass nothing as the target to carry nothing.

The target does not need to be persistent. It is carried for this one crossing only, and afterwards it goes back to being whatever it was. A target that no longer exists is treated as nothing.

Persistent instances, and what a room does not remember

An instance can be marked persistent, which means it survives every room change instead of being destroyed. A persistent player object is the usual example. It receives the new room's Room Start event like everything else. See persistent.

The setting above belongs to one instance. An ordinary room is rebuilt from its authored data every time you enter it. Come back to it and the enemies you defeated are back, the door you opened is shut again, and anything you moved is where you first placed it. If the game should remember any of that, write it down yourself with save_set, or with a global variable, and have the room's code read it when it loads. See Saving.

Or mark the whole room Persistent. A persistent room is kept when you leave it, with every object as it was, and the enemies you defeated stay defeated. Persistent rooms says what that keeps, what runs when you come back, and what it costs.

Doors

Doors are ordinary objects placed by the editor's Room Editor. Each has a number, door_id, and it can be given a name. Five functions work on them without needing a reference to the door itself:

A door named front_gate is written door_lock(front_gate). The name turns into its number when the game is built, and a name that does not exist is a build error rather than a call that does nothing. A locked door still works as an exit, so a player can still arrive through it. A door only exists while its room is loaded, so a door in another room is not found. Locking or asking about it does nothing, and asking gives back "not locked" and DOOR_TWO_WAY. Rooms describes doors in full.

Where a door lands the player

A linked door puts the player on the middle of its partner door, so the partner has to stand on open floor just inside its room. A door whose landing is outside its room or inside a solid tile is an error in the Problems list and stops the build (the player would arrive stuck in the wall): move the partner door to open floor. A pickup placed inside a solid tile gets a warning, because nobody can touch it.

Example

// Collision event of a doorway object
room_change_ext(room_cellar, other, 24, 120, wavy_wipe.south);

The instance that touched the doorway, other, is carried into room_cellar and placed at (24, 120), counted from the cellar's top-left corner, with a wavy wipe sweeping south.

// Step event of a "next level" object
if (btn_pressed(BTN_START)) {
    room_next();
}

Pressing START moves to the next room in order, using the current room's own on-exit transition, and wraps to the first room after the last.

See also: Roomsroom_changeroom_change_extdoor_lockpersistentPersistent roomsSaving a game